home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / text / faqs / unix-faq.faq.part2 < prev    next >
Encoding:
Internet Message Format  |  1994-05-16  |  33.1 KB

  1. Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
  2. Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
  3. From: tmatimar@isgtec.com (Ted Timar)
  4. Date: 14 May 1994 10:18:42 GMT
  5.  
  6. Archive-name: unix-faq/faq/part2
  7. Version: $Id: part2,v 2.5 1994/04/28 19:25:03 tmatimar Exp $
  8.  
  9. These seven articles contain the answers to some Frequently Asked
  10. Questions often seen in comp.unix.questions and comp.unix.shell.
  11. Please don't ask these questions again, they've been answered plenty
  12. of times already - and please don't flame someone just because they may
  13. not have read this particular posting.  Thank you.
  14.  
  15. This collection of documents is Copyright (c) 1994, Ted Timar.
  16. All rights reserved.  Permission to distribute the collection is
  17. hereby granted providing that distribution is electronic, no money
  18. is involved, reasonable attempts are made to use the latest version
  19. and all credits and this copyright notice are maintained.
  20. Other requests for distribution will be considered.
  21.  
  22. All information here has been contributed with good intentions, but
  23. none of it is guaranteed either by the contributors or myself to be
  24. accurate.  The users of this information take all responsibility for
  25. any damage that may occur.
  26.  
  27. Many FAQs, including this one, are available on the archive site
  28. rtfm.mit.edu in the directory pub/usenet/news.answers.
  29. The name under which a FAQ is archived appears in the "Archive-Name:"
  30. line at the top of the article.  This FAQ is archived as
  31. "unix-faq/faq/part[1-7]".
  32.  
  33. These articles are divided approximately as follows:
  34.  
  35.       1.*) General questions.
  36.       2.*) Relatively basic questions, likely to be asked by beginners.
  37.       3.*) Intermediate questions.
  38.       4.*) Advanced questions, likely to be asked by people who thought
  39.        they already knew all of the answers.
  40.       5.*) Questions pertaining to the various shells, and the differences.
  41.       6.*) An overview of Unix variants.
  42.       7.*) An comparison of configuration management systems (RCS, SCCS).
  43.  
  44. This article includes answers to:
  45.  
  46.       2.1)  How do I remove a file whose name begins with a "-" ?
  47.       2.2)  How do I remove a file with funny characters in the filename ?
  48.       2.3)  How do I get a recursive directory listing?
  49.       2.4)  How do I get the current directory into my prompt?
  50.       2.5)  How do I read characters from the terminal in a shell script?
  51.       2.6)  How do I rename "*.foo" to "*.bar", or change file names
  52.               to lowercase?
  53.       2.7)  Why do I get [some strange error message] when I
  54.               "rsh host command" ?
  55.       2.8)  How do I {set an environment variable, change directory} inside a
  56.               program or shell script and have that change affect my
  57.               current shell?
  58.       2.9)  How do I redirect stdout and stderr separately in csh?
  59.       2.10) How do I tell inside .cshrc if I'm a login shell?
  60.       2.11) How do I construct a shell glob-pattern that matches all files
  61.             except "." and ".." ?
  62.       2.12) How do I find the last argument in a Bourne shell script?
  63.       2.13) What's wrong with having '.' in your $PATH ?
  64.       2.14) How do I ring the terminal bell during a shell script?
  65.       2.15) Why can't I use "talk" to talk with my friend on machine X?
  66.  
  67. If you're looking for the answer to, say, question 2.5, and want to skip
  68. everything else, you can search ahead for the regular expression "^2.5)".
  69.  
  70. While these are all legitimate questions, they seem to crop up in
  71. comp.unix.questions or comp.unix.shell on an annual basis, usually
  72. followed by plenty of replies (only some of which are correct) and then
  73. a period of griping about how the same questions keep coming up.  You
  74. may also like to read the monthly article "Answers to Frequently Asked
  75. Questions" in the newsgroup "news.announce.newusers", which will tell
  76. you what "UNIX" stands for.
  77.  
  78. With the variety of Unix systems in the world, it's hard to guarantee
  79. that these answers will work everywhere.  Read your local manual pages
  80. before trying anything suggested here.  If you have suggestions or
  81. corrections for any of these answers, please send them to to
  82. tmatimar@isgtec.com.
  83.  
  84. ----------------------------------------------------------------------
  85.  
  86. Subject: How do I remove a file whose name begins with a "-" ?
  87. Date: Thu Mar 18 17:16:55 EST 1993
  88.  
  89. 2.1)  How do I remove a file whose name begins with a "-" ?
  90.  
  91.       Figure out some way to name the file so that it doesn't begin
  92.       with a dash.  The simplest answer is to use
  93.  
  94.         rm ./-filename
  95.  
  96.       (assuming "-filename" is in the current directory, of course.)
  97.       This method of avoiding the interpretation of the "-" works with
  98.       other commands too.
  99.  
  100.       Many commands, particularly those that have been written to use
  101.       the "getopt(3)" argument parsing routine, accept a "--" argument
  102.       which means "this is the last option, anything after this is not
  103.       an option", so your version of rm might handle "rm -- -filename".
  104.       Some versions of rm that don't use getopt() treat a single "-"
  105.       in the same way, so you can also try "rm - -filename".
  106.  
  107. ------------------------------
  108.  
  109. Subject: How do I remove a file with funny characters in the filename ?
  110. Date: Thu Mar 18 17:16:55 EST 1993
  111.  
  112. 2.2)  How do I remove a file with funny characters in the filename ?
  113.  
  114.       If the 'funny character' is a '/', skip to the last part of this
  115.       answer.  If the funny character is something else, such as a ' '
  116.       or control character or character with the 8th bit set, keep reading.
  117.  
  118.       The classic answers are
  119.  
  120.     rm -i some*pattern*that*matches*only*the*file*you*want
  121.  
  122.     which asks you whether you want to remove each file matching
  123.     the indicated pattern;  depending on your shell, this may not
  124.     work if the filename has a character with the 8th bit set (the
  125.     shell may strip that off);
  126.  
  127.       and
  128.  
  129.     rm -ri .
  130.  
  131.     which asks you whether to remove each file in the directory.
  132.     Answer "y" to the problem file and "n" to everything else.
  133.     Unfortunately this doesn't work with many versions of rm.  Also
  134.     unfortunately, this will walk through every subdirectory of ".",
  135.     so you might want to "chmod a-x" those directories temporarily
  136.     to make them unsearchable.
  137.  
  138.     Always take a deep breath and think about what you're doing and
  139.     double check what you typed when you use rm's "-r" flag or a
  140.     wildcard on the command line;
  141.  
  142.       and
  143.  
  144.     find . -type f ... -ok rm '{}' \;
  145.  
  146.       where "..." is a group of predicates that uniquely identify the
  147.       file.  One possibility is to figure out the inode number of the
  148.       problem file (use "ls -i .") and then use
  149.  
  150.     find . -inum 12345 -ok rm '{}' \;
  151.  
  152.       or
  153.     find . -inum 12345 -ok mv '{}' new-file-name \;
  154.     
  155.       "-ok" is a safety check - it will prompt you for confirmation of
  156.       the command it's about to execute.  You can use "-exec" instead
  157.       to avoid the prompting, if you want to live dangerously, or if
  158.       you suspect that the filename may contain a funny character
  159.       sequence that will mess up your screen when printed.
  160.  
  161.       What if the filename has a '/' in it?
  162.  
  163.       These files really are special cases, and can only be created by
  164.       buggy kernel code (typically by implementations of NFS that don't
  165.       filter out illegal characters in file names from remote
  166.       machines.)  The first thing to do is to try to understand exactly
  167.       why this problem is so strange.
  168.  
  169.       Recall that Unix directories are simply pairs of filenames and
  170.       inode numbers.  A directory essentially contains information
  171.       like this:
  172.  
  173.     filename  inode
  174.  
  175.     file1      12345
  176.     file2.c      12349
  177.     file3     12347
  178.  
  179.       Theoretically, '/' and '\0' are the only two characters that
  180.       cannot appear in a filename - '/' because it's used to separate
  181.       directories and files, and '\0' because it terminates a filename.
  182.  
  183.       Unfortunately some implementations of NFS will blithely create
  184.       filenames with embedded slashes in response to requests from
  185.       remote machines.  For instance, this could happen when someone on
  186.       a Mac or other non-Unix machine decides to create a remote NFS
  187.       file on your Unix machine with the date in the filename.  Your
  188.       Unix directory then has this in it:
  189.  
  190.     filename  inode
  191.  
  192.     91/02/07  12357
  193.  
  194.       No amount of messing around with 'find' or 'rm' as described
  195.       above will delete this file, since those utilities and all other
  196.       Unix programs, are forced to interpret the '/' in the normal way.
  197.  
  198.       Any ordinary program will eventually try to do
  199.       unlink("91/02/07"), which as far as the kernel is concerned means
  200.       "unlink the file 07 in the subdirectory 02 of directory 91", but
  201.       that's not what we have - we have a *FILE* named "91/02/07" in
  202.       the current directory.  This is a subtle but crucial distinction.
  203.  
  204.       What can you do in this case?  The first thing to try is to
  205.       return to the Mac that created this crummy entry, and see if you
  206.       can convince it and your local NFS daemon to rename the file to
  207.       something without slashes.
  208.  
  209.       If that doesn't work or isn't possible, you'll need help from
  210.       your system manager, who will have to try the one of the
  211.       following.  Use "ls -i" to find the inode number of this bogus
  212.       file, then unmount the file system and use "clri" to clear the
  213.       inode, and "fsck" the file system with your fingers crossed.
  214.       This destroys the information in the file.  If you want to keep
  215.       it, you can try:
  216.  
  217.     create a new directory in the same parent directory as the one
  218.     containing the bad file name;
  219.  
  220.     move everything you can (i.e. everything but the file with the
  221.     bad name) from the old directory to the new one;
  222.  
  223.     do "ls -id" on the directory containing the file with the bad
  224.     name to get its inumber;
  225.  
  226.     umount the file system;
  227.  
  228.     "clri" the directory containing the file with the bad name;
  229.  
  230.     "fsck" the file system.
  231.  
  232.       Then, to find the file,
  233.  
  234.     remount the file system;
  235.  
  236.     rename the directory you created to have the name of the old
  237.     directory (since the old directory should have been blown away
  238.     by "fsck")
  239.  
  240.     move the file out of "lost+found" into the directory with a
  241.     better name.
  242.  
  243.       Alternatively, you can patch the directory the hard way by
  244.       crawling around in the raw file system.  Use "fsdb", if you
  245.       have it.
  246.  
  247. ------------------------------
  248.  
  249. Subject: How do I get a recursive directory listing?
  250. Date: Thu Mar 18 17:16:55 EST 1993
  251.  
  252. 2.3)  How do I get a recursive directory listing?
  253.  
  254.       One of the following may do what you want:
  255.  
  256.     ls -R             (not all versions of "ls" have -R)
  257.     find . -print        (should work everywhere)
  258.     du -a .            (shows you both the name and size)
  259.  
  260.       If you're looking for a wildcard pattern that will match all ".c"
  261.       files in this directory and below, you won't find one, but you
  262.       can use
  263.  
  264.     % some-command `find . -name '*.c' -print`
  265.  
  266.       "find" is a powerful program.  Learn about it.
  267.  
  268. ------------------------------
  269.  
  270. Subject: How do I get the current directory into my prompt?
  271. Date: Thu Mar 18 17:16:55 EST 1993
  272.  
  273. 2.4)  How do I get the current directory into my prompt?
  274.  
  275.       It depends which shell you are using.  It's easy with some
  276.       shells, hard or impossible with others.
  277.  
  278.       C Shell (csh):
  279.     Put this in your .cshrc - customize the prompt variable the
  280.     way you want.
  281.  
  282.         alias setprompt 'set prompt="${cwd}% "'
  283.         setprompt        # to set the initial prompt
  284.         alias cd 'chdir \!* && setprompt'
  285.     
  286.     If you use pushd and popd, you'll also need
  287.  
  288.         alias pushd 'pushd \!* && setprompt'
  289.         alias popd  'popd  \!* && setprompt'
  290.  
  291.     Some C shells don't keep a $cwd variable - you can use
  292.     `pwd` instead.
  293.  
  294.     If you just want the last component of the current directory
  295.     in your prompt ("mail% " instead of "/usr/spool/mail% ")
  296.     you can use
  297.  
  298.         alias setprompt 'set prompt="$cwd:t% "'
  299.     
  300.     Some older csh's get the meaning of && and || reversed.
  301.     Try doing:
  302.  
  303.         false && echo bug
  304.  
  305.     If it prints "bug", you need to switch && and || (and get
  306.     a better version of csh.)
  307.  
  308.       Bourne Shell (sh):
  309.  
  310.     If you have a newer version of the Bourne Shell (SVR2 or newer)
  311.     you can use a shell function to make your own command, "xcd" say:
  312.  
  313.         xcd() { cd $* ; PS1="`pwd` $ "; }
  314.  
  315.     If you have an older Bourne shell, it's complicated but not
  316.     impossible.  Here's one way.  Add this to your .profile file:
  317.  
  318.         LOGIN_SHELL=$$ export LOGIN_SHELL
  319.         CMDFILE=/tmp/cd.$$ export CMDFILE
  320.         # 16 is SIGURG, pick a signal that's not likely to be used
  321.         PROMPTSIG=16 export PROMPTSIG
  322.         trap '. $CMDFILE' $PROMPTSIG
  323.  
  324.     and then put this executable script (without the indentation!),
  325.     let's call it "xcd", somewhere in your PATH
  326.  
  327.         : xcd directory - change directory and set prompt
  328.         : by signalling the login shell to read a command file
  329.         cat >${CMDFILE?"not set"} <<EOF
  330.         cd $1
  331.         PS1="\`pwd\`$ "
  332.         EOF
  333.         kill -${PROMPTSIG?"not set"} ${LOGIN_SHELL?"not set"}
  334.  
  335.     Now change directories with "xcd /some/dir".
  336.  
  337.       Korn Shell (ksh):
  338.  
  339.     Put this in your .profile file:
  340.         PS1='$PWD $ '
  341.     
  342.     If you just want the last component of the directory, use
  343.         PS1='${PWD##*/} $ '
  344.  
  345.       T C shell (tcsh)
  346.  
  347.     Tcsh is a popular enhanced version of csh with some extra
  348.     builtin variables (and many other features):
  349.  
  350.         %~        the current directory, using ~ for $HOME
  351.         %/        the full pathname of the current directory
  352.         %c or %.    the trailing component of the current directory
  353.  
  354.     so you can do
  355.  
  356.         set prompt='%~ '
  357.  
  358.       BASH (FSF's "Bourne Again SHell")
  359.     
  360.     \w in $PS1 gives the full pathname of the current directory,
  361.     with ~ expansion for $HOME;  \W gives the basename of
  362.     the current directory.  So, in addition to the above sh and
  363.     ksh solutions, you could use
  364.  
  365.         PS1='\w $ '    
  366.     or
  367.         PS1='\W $ '
  368.  
  369. ------------------------------
  370.  
  371. Subject: How do I read characters from the terminal in a shell script?
  372. Date: Thu Mar 18 17:16:55 EST 1993
  373.  
  374. 2.5)  How do I read characters from the terminal in a shell script?
  375.  
  376.       In sh, use read.  It is most common to use a loop like
  377.  
  378.         while read line
  379.         do
  380.             ...
  381.         done
  382.  
  383.       In csh, use $< like this:
  384.     
  385.         while ( 1 )
  386.         set line = "$<"
  387.         if ( "$line" == "" ) break
  388.         ...
  389.         end
  390.  
  391.       Unfortunately csh has no way of distinguishing between a blank
  392.       line and an end-of-file.
  393.  
  394.       If you're using sh and want to read a *single* character from the
  395.       terminal, you can try something like
  396.  
  397.         echo -n "Enter a character: "
  398.         stty cbreak        # or  stty raw
  399.         readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
  400.         stty -cbreak
  401.  
  402.         echo "Thank you for typing a $readchar ."
  403.  
  404. ------------------------------
  405.  
  406. Subject: How do I rename "*.foo" to "*.bar", or change file names to lowercase?
  407. Date: Thu Mar 18 17:16:55 EST 1993
  408.  
  409. 2.6)  How do I rename "*.foo" to "*.bar", or change file names to lowercase?
  410.     
  411.       Why doesn't "mv *.foo *.bar" work?  Think about how the shell
  412.       expands wildcards.   "*.foo" and "*.bar" are expanded before the
  413.       mv command ever sees the arguments.  Depending on your shell,
  414.       this can fail in a couple of ways.  CSH prints "No match."
  415.       because it can't match "*.bar".  SH executes "mv a.foo b.foo
  416.       c.foo *.bar", which will only succeed if you happen to have a
  417.       single directory named "*.bar", which is very unlikely and almost
  418.       certainly not what you had in mind.
  419.  
  420.       Depending on your shell, you can do it with a loop to "mv" each
  421.       file individually.  If your system has "basename", you can use:
  422.  
  423.       C Shell:
  424.     foreach f ( *.foo )
  425.         set base=`basename $f .foo`
  426.         mv $f $base.bar
  427.     end
  428.  
  429.       Bourne Shell:
  430.     for f in *.foo; do
  431.         base=`basename $f .foo`
  432.         mv $f $base.bar
  433.     done
  434.  
  435.       Some shells have their own variable substitution features, so
  436.       instead of using "basename", you can use simpler loops like:
  437.  
  438.       C Shell:
  439.  
  440.     foreach f ( *.foo )
  441.         mv $f $f:r.bar
  442.     end
  443.  
  444.       Korn Shell:
  445.  
  446.     for f in *.foo; do
  447.         mv $f ${f%foo}bar
  448.     done
  449.  
  450.       If you don't have "basename" or want to do something like
  451.       renaming foo.* to bar.*, you can use something like "sed" to
  452.       strip apart the original file name in other ways, but the general
  453.       looping idea is the same.  You can also convert file names into
  454.       "mv" commands with 'sed', and hand the commands off to "sh" for
  455.       execution.  Try
  456.  
  457.     ls -d *.foo | sed -e 's/.*/mv & &/' -e 's/foo$/bar/' | sh
  458.  
  459.       A program by Vladimir Lanin called "mmv" that does this job
  460.       nicely was posted to comp.sources.unix (Volume 21, issues 87 and
  461.       88) in April 1990.  It lets you use
  462.  
  463.     mmv '*.foo' '=1.bar'
  464.  
  465.       Shell loops like the above can also be used to translate file
  466.       names from upper to lower case or vice versa.  You could use
  467.       something like this to rename uppercase files to lowercase:
  468.  
  469.     C Shell:
  470.         foreach f ( * )
  471.         mv $f `echo $f | tr '[A-Z]' '[a-z]'`
  472.         end
  473.     Bourne Shell:
  474.         for f in *; do
  475.         mv $f `echo $f | tr '[A-Z]' '[a-z]'`
  476.         done
  477.     Korn Shell:
  478.         typeset -l l
  479.         for f in *; do
  480.         l="$f"
  481.         mv $f $l
  482.         done
  483.  
  484.       If you wanted to be really thorough and handle files with `funny'
  485.       names (embedded blanks or whatever) you'd need to use
  486.  
  487.     Bourne Shell:
  488.  
  489.         for f in *; do
  490.           g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
  491.           mv "$f" "$g"
  492.         done
  493.  
  494.       The `expr' command will always print the filename, even if it
  495.       equals `-n' or if it contains a System V escape sequence like `\c'.
  496.  
  497.       Some versions of "tr" require the [ and ], some don't.  It
  498.       happens to be harmless to include them in this particular
  499.       example; versions of tr that don't want the [] will conveniently
  500.       think they are supposed to translate '[' to '[' and ']' to ']'.
  501.  
  502.       If you have the "perl" language installed, you may find this
  503.       rename script by Larry Wall very useful.  It can be used to
  504.       accomplish a wide variety of filename changes.
  505.  
  506.     #!/usr/bin/perl
  507.     #
  508.     # rename script examples from lwall:
  509.     #       rename 's/\.orig$//' *.orig
  510.     #       rename 'y/A-Z/a-z/ unless /^Make/' *
  511.     #       rename '$_ .= ".bad"' *.f
  512.     #       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *
  513.  
  514.     $op = shift;
  515.     for (@ARGV) {
  516.         $was = $_;
  517.         eval $op;
  518.         die $@ if $@;
  519.         rename($was,$_) unless $was eq $_;
  520.     }
  521.  
  522. ------------------------------
  523.  
  524. Subject: Why do I get [some strange error message] when I "rsh host command" ?
  525. Date: Thu Mar 18 17:16:55 EST 1993
  526.  
  527. 2.7)  Why do I get [some strange error message] when I "rsh host command" ?
  528.  
  529.       (We're talking about the remote shell program "rsh" or sometimes
  530.       "remsh" or "remote"; on some machines, there is a restricted shell
  531.       called "rsh", which is a different thing.)
  532.  
  533.       If your remote account uses the C shell, the remote host will
  534.       fire up a C shell to execute 'command' for you, and that shell
  535.       will read your remote .cshrc file.  Perhaps your .cshrc contains
  536.       a "stty", "biff" or some other command that isn't appropriate for
  537.       a non-interactive shell.  The unexpected output or error message
  538.       from these commands can screw up your rsh in odd ways.
  539.  
  540.       Here's an example.  Suppose you have
  541.  
  542.     stty erase ^H
  543.     biff y
  544.  
  545.       in your .cshrc file.  You'll get some odd messages like this.
  546.  
  547.     % rsh some-machine date
  548.     stty: : Can't assign requested address
  549.     Where are you?
  550.     Tue Oct  1 09:24:45 EST 1991
  551.  
  552.       You might also get similar errors when running certain "at" or
  553.       "cron" jobs that also read your .cshrc file.
  554.  
  555.       Fortunately, the fix is simple.  There are, quite possibly, a
  556.       whole *bunch* of operations in your ".cshrc" (e.g., "set
  557.       history=N") that are simply not worth doing except in interactive
  558.       shells.  What you do is surround them in your ".cshrc" with:
  559.  
  560.         if ( $?prompt ) then
  561.             operations....
  562.         endif
  563.  
  564.       and, since in a non-interactive shell "prompt" won't be set, the
  565.       operations in question will only be done in interactive shells.
  566.  
  567.       You may also wish to move some commands to your .login file; if
  568.       those commands only need to be done when a login session starts
  569.       up (checking for new mail, unread news and so on) it's better to
  570.       have them in the .login file.
  571.  
  572. ------------------------------
  573.  
  574. Subject: How do I ... and have that change affect my current shell?
  575. Date: Thu Mar 18 17:16:55 EST 1993
  576.  
  577. 2.8)  How do I {set an environment variable, change directory} inside
  578.       a program or shell script and have that change affect my
  579.       current shell?
  580.  
  581.       In general, you can't, at least not without making special
  582.       arrangements.  When a child process is created, it inherits a
  583.       copy of its parent's variables (and current directory).  The
  584.       child can change these values all it wants but the changes won't
  585.       affect the parent shell, since the child is changing a copy of
  586.       the original data.
  587.  
  588.       Some special arrangements are possible.  Your child process could
  589.       write out the changed variables, if the parent was prepared to
  590.       read the output and interpret it as commands to set its own
  591.       variables.
  592.  
  593.       Also, shells can arrange to run other shell scripts in the
  594.       context of the current shell, rather than in a child process, so
  595.       that changes will affect the original shell.
  596.  
  597.       For instance, if you have a C shell script named "myscript":
  598.  
  599.     cd /very/long/path
  600.     setenv PATH /something:/something-else
  601.  
  602.       or the equivalent Bourne or Korn shell script
  603.  
  604.     cd /very/long/path
  605.     PATH=/something:/something-else export PATH
  606.  
  607.       and try to run "myscript" from your shell, your shell will fork
  608.       and run the shell script in a subprocess.  The subprocess is also
  609.       running the shell; when it sees the "cd" command it changes *its*
  610.       current directory, and when it sees the "setenv" command it
  611.       changes *its* environment, but neither has any effect on the
  612.       current directory of the shell at which you're typing (your login
  613.       shell, let's say).
  614.  
  615.       In order to get your login shell to execute the script (without
  616.       forking) you have to use the "." command (for the Bourne or Korn
  617.       shells) or the "source" command (for the C shell).  I.e. you type
  618.  
  619.     . myscript
  620.  
  621.       to the Bourne or Korn shells, or
  622.  
  623.     source myscript
  624.  
  625.       to the C shell.
  626.  
  627.       If all you are trying to do is change directory or set an
  628.       environment variable, it will probably be simpler to use a C
  629.       shell alias or Bourne/Korn shell function.  See the "how do I get
  630.       the current directory into my prompt" section of this article for
  631.       some examples.
  632.  
  633.       A much more detailed answer prepared by
  634.       xtm@telelogic.se (Thomas Michanek) can be found at
  635.       ftp.wg.omron.co.jp in /pub/unix-faq/docs/script-vs-env.
  636.  
  637. ------------------------------
  638.  
  639. Subject: How do I redirect stdout and stderr separately in csh?
  640. From: msb@sq.com (Mark Brader)
  641. Date: Mon, 26 Oct 1992 20:15:00 -0500
  642.  
  643. 2.9)  How do I redirect stdout and stderr separately in csh?
  644.  
  645.       In csh, you can redirect stdout with ">", or stdout and stderr
  646.       together with ">&" but there is no direct way to redirect stderr
  647.       only.  The best you can do is
  648.  
  649.     ( command >stdout_file ) >&stderr_file
  650.  
  651.       which runs "command" in a subshell;  stdout is redirected inside
  652.       the subshell to stdout_file, and both stdout and stderr from the
  653.       subshell are redirected to stderr_file, but by this point stdout
  654.       has already been redirected so only stderr actually winds up in
  655.       stderr_file.
  656.  
  657.       If what you want is to avoid redirecting stdout at all, let sh
  658.       do it for you.
  659.  
  660.     sh -c 'command 2>stderr_file'
  661.  
  662. ------------------------------
  663.  
  664. Subject: How do I tell inside .cshrc if I'm a login shell?
  665. Date: Thu Mar 18 17:16:55 EST 1993
  666.  
  667. 2.10) How do I tell inside .cshrc if I'm a login shell?
  668.  
  669.       When people ask this, they usually mean either
  670.  
  671.     How can I tell if it's an interactive shell?  or
  672.     How can I tell if it's a top-level shell?
  673.  
  674.       You could perhaps determine if your shell truly is a login shell
  675.       (i.e. is going to source ".login" after it is done with ".cshrc")
  676.       by fooling around with "ps" and "$$".  Login shells generally
  677.       have names that begin with a '-'.  If you're really interested in
  678.       the other two questions, here's one way you can organize your
  679.       .cshrc to find out.
  680.  
  681.     if (! $?CSHLEVEL) then
  682.         #
  683.         # This is a "top-level" shell,
  684.         # perhaps a login shell, perhaps a shell started up by
  685.         # 'rsh machine some-command'
  686.         # This is where we should set PATH and anything else we
  687.         # want to apply to every one of our shells.
  688.         #
  689.         setenv      CSHLEVEL        0
  690.         set home = ~username        # just to be sure
  691.         source ~/.env               # environment stuff we always want
  692.     else
  693.         #
  694.         # This shell is a child of one of our other shells so
  695.         # we don't need to set all the environment variables again.
  696.         #
  697.         set tmp = $CSHLEVEL
  698.         @ tmp++
  699.         setenv      CSHLEVEL        $tmp
  700.     endif
  701.  
  702.     # Exit from .cshrc if not interactive, e.g. under rsh
  703.     if (! $?prompt) exit
  704.  
  705.     # Here we could set the prompt or aliases that would be useful
  706.     # for interactive shells only.
  707.  
  708.     source ~/.aliases
  709.  
  710. ------------------------------
  711.  
  712. Subject: How do I construct a ... matches all files except "." and ".." ?
  713. Date: Thu Mar 18 17:16:55 EST 1993
  714.  
  715. 2.11) How do I construct a shell glob-pattern that matches all files
  716.       except "." and ".." ?
  717.  
  718.       You'd think this would be easy.
  719.  
  720.       *        Matches all files that don't begin with a ".";
  721.  
  722.       .*         Matches all files that do begin with a ".", but
  723.          this includes the special entries "." and "..",
  724.          which often you don't want;
  725.  
  726.       .[!.]*   (Newer shells only; some shells use a "^" instead of
  727.          the "!"; POSIX shells must accept the "!", but may
  728.          accept a "^" as well; all portable applications shall
  729.          not use an unquoted "^" immediately following the "[")
  730.  
  731.          Matches all files that begin with a "." and are
  732.          followed by a non-"."; unfortunately this will miss
  733.          "..foo";
  734.  
  735.       .??*     Matches files that begin with a "." and which are
  736.          at least 3 characters long.  This neatly avoids
  737.          "." and "..", but also misses ".a" .
  738.  
  739.       So to match all files except "." and ".." safely you have to use
  740.       3 patterns (if you don't have filenames like ".a" you can leave
  741.       out the first):
  742.  
  743.     .[!.]* .??* *
  744.  
  745.       Alternatively you could employ an external program or two and use
  746.       backquote substitution.  This is pretty good:
  747.  
  748.       `ls -a | sed -e '/^\.$/d' -e '/^\.\.$/d'`
  749.  
  750.     (or `ls -A` in some Unix versions)
  751.  
  752.       but even it will mess up on files with newlines, IFS characters
  753.       or wildcards in their names.
  754.  
  755. ------------------------------
  756.  
  757. Subject: How do I find the last argument in a Bourne shell script?
  758. Date: Thu Mar 18 17:16:55 EST 1993
  759.  
  760. 2.12) How do I find the last argument in a Bourne shell script?
  761.  
  762.       Answer by:
  763.     Martin Weitzel <@mikros.systemware.de:martin@mwtech.uucp>
  764.     Maarten Litmaath <maart@nat.vu.nl>
  765.  
  766.       If you are sure the number of arguments is at most 9, you can use:
  767.  
  768.     eval last=\${$#}
  769.  
  770.       In POSIX-compatible shells it works for ANY number of arguments.
  771.       The following works always too:
  772.  
  773.     for last
  774.     do
  775.         :
  776.     done
  777.  
  778.       This can be generalized as follows:
  779.  
  780.     for i
  781.     do
  782.         third_last=$second_last
  783.         second_last=$last
  784.         last=$i
  785.     done
  786.  
  787.       Now suppose you want to REMOVE the last argument from the list,
  788.       or REVERSE the argument list, or ACCESS the N-th argument
  789.       directly, whatever N may be.  Here is a basis of how to do it,
  790.       using only built-in shell constructs, without creating subprocesses:
  791.  
  792.     t0= u0= rest='1 2 3 4 5 6 7 8 9' argv=
  793.  
  794.     for h in '' $rest
  795.     do
  796.         for t in "$t0" $rest
  797.         do
  798.             for u in $u0 $rest
  799.             do
  800.                 case $# in
  801.                 0)
  802.                     break 3
  803.                 esac
  804.                 eval argv$h$t$u=\$1
  805.                 argv="$argv \"\$argv$h$t$u\""    # (1)
  806.                 shift
  807.             done
  808.             u0=0
  809.         done
  810.         t0=0
  811.     done
  812.  
  813.     # now restore the arguments
  814.     eval set x "$argv"                    # (2)
  815.     shift
  816.  
  817.       This example works for the first 999 arguments.  Enough?
  818.       Take a good look at the lines marked (1) and (2) and convince
  819.       yourself that the original arguments are restored indeed, no
  820.       matter what funny characters they contain!
  821.  
  822.       To find the N-th argument now you can use this:
  823.  
  824.     eval argN=\$argv$N
  825.  
  826.       To reverse the arguments the line marked (1) must be changed to:
  827.  
  828.     argv="\"\$argv$h$t$u\" $argv"
  829.  
  830.       How to remove the last argument is left as an exercise.
  831.  
  832.       If you allow subprocesses as well, possibly executing nonbuilt-in
  833.       commands, the `argvN' variables can be set up more easily:
  834.  
  835.     N=1
  836.  
  837.     for i
  838.     do
  839.         eval argv$N=\$i
  840.         N=`expr $N + 1`
  841.     done
  842.  
  843.       To reverse the arguments there is still a simpler method, that
  844.       even does not create subprocesses.  This approach can also be
  845.       taken if you want to delete e.g. the last argument, but in that
  846.       case you cannot refer directly to the N-th argument any more,
  847.       because the `argvN' variables are set up in reverse order:
  848.  
  849.     argv=
  850.  
  851.     for i
  852.     do
  853.         eval argv$#=\$i
  854.         argv="\"\$argv$#\" $argv"
  855.         shift
  856.     done
  857.  
  858.     eval set x "$argv"
  859.     shift
  860.  
  861. ------------------------------
  862.  
  863. Subject: What's wrong with having '.' in your $PATH ?
  864. Date: Thu Mar 18 17:16:55 EST 1993
  865.  
  866. 2.13) What's wrong with having '.' in your $PATH ?
  867.  
  868.       A bit of background: the PATH environment variable is a list of
  869.       directories separated by colons.  When you type a command name
  870.       without giving an explicit path (e.g. you type "ls", rather than
  871.       "/bin/ls") your shell searches each directory in the PATH list in
  872.       order, looking for an executable file by that name, and the shell
  873.       will run the first matching program it finds.
  874.  
  875.       One of the directories in the PATH list can be the current
  876.       directory "." .  It is also permissible to use an empty directory
  877.       name in the PATH list to indicate the current directory.  Both of
  878.       these are equivalent
  879.  
  880.       for csh users:
  881.  
  882.     setenv PATH :/usr/ucb:/bin:/usr/bin
  883.     setenv PATH .:/usr/ucb:/bin:/usr/bin
  884.  
  885.       for sh or ksh users
  886.  
  887.     PATH=:/usr/ucb:/bin:/usr/bin export PATH
  888.     PATH=.:/usr/ucb:/bin:/usr/bin export PATH
  889.  
  890.       Having "." somewhere in the PATH is convenient - you can type
  891.       "a.out" instead of "./a.out" to run programs in the current
  892.       directory.  But there's a catch.
  893.  
  894.       Consider what happens in the case  where "." is the first entry
  895.       in the PATH.  Suppose your current directory is a publically-
  896.       writable one, such as "/tmp".  If there just happens to be a
  897.       program named "/tmp/ls" left there by some other user, and you
  898.       type "ls" (intending, of course, to run the normal "/bin/ls"
  899.       program), your shell will instead run "./ls", the other user's
  900.       program.  Needless to say, the results of running an unknown
  901.       program like this might surprise you.
  902.  
  903.       It's slightly better to have "." at the end of the PATH:
  904.  
  905.     setenv PATH /usr/ucb:/bin:/usr/bin:.
  906.  
  907.       Now if you're in /tmp and you type "ls", the shell will
  908.       search /usr/ucb, /bin and /usr/bin for a program named
  909.       "ls" before it gets around to looking in ".", and there
  910.       is less risk of inadvertently running some other user's
  911.       "ls" program.  This isn't 100% secure though - if you're
  912.       a clumsy typist and some day type "sl -l" instead of "ls -l",
  913.       you run the risk of running "./sl", if there is one.
  914.       Some "clever" programmer could anticipate common typing
  915.       mistakes and leave programs by those names scattered
  916.       throughout public directories.  Beware.
  917.  
  918.       Many seasoned Unix users get by just fine without having
  919.       "." in the PATH at all:
  920.  
  921.     setenv PATH /usr/ucb:/bin:/usr/bin
  922.  
  923.       If you do this, you'll need to type "./program" instead
  924.       of "program" to run programs in the current directory, but
  925.       the increase in security is probably worth it.
  926.  
  927. ------------------------------
  928.  
  929. Subject: How do I ring the terminal bell during a shell script?
  930. From: uwe@mpi-sb.mpg.de (Uwe Waldmann)
  931. Date: Fri, 30 Apr 93 16:33:00 +0200
  932.  
  933. 2.9)  How do I ring the terminal bell during a shell script?
  934.  
  935.       The answer depends on your Unix version (or rather on the kind of
  936.       "echo" program that is available on your machine).
  937.  
  938.       A BSD-like "echo" uses the "-n" option for suppressing the final
  939.       newline and does not understand the octal \nnn notation.  Thus
  940.       the command is
  941.  
  942.         echo -n '^G'
  943.  
  944.       where ^G means a _literal_ BEL-character (you can produce this in
  945.       emacs using "Ctrl-Q Ctrl-G" and in vi using "Ctrl-V Ctrl-G").
  946.  
  947.       A SysV-like "echo" understands the \nnn notation and uses \c to
  948.       suppress the final newline, so the answer is:
  949.  
  950.         echo '\007\c'
  951.  
  952. ------------------------------
  953.  
  954. Subject: Why can't I use "talk" to talk with my friend on machine X?
  955. From: tmatimar@isgtec.com (Ted Timar)
  956. Date: Thu Mar 18 17:16:55 EST 1993
  957.  
  958. 2.15) Why can't I use "talk" to talk with my friend on machine X?
  959.  
  960.       Unix has three common "talk" programs, none of which can talk with
  961.       any of the others.  The "old" talk accounts for the first two types.
  962.       This version (often called otalk) did not take "endian" order into
  963.       account when talking to other machines.  As a consequence, the Vax
  964.       version of otalk cannot talk with the Sun version of otalk.
  965.       These versions of talk use port 517.
  966.  
  967.       Around 1987, most vendors (except Sun, who took 6 years longer than
  968.       any of their competitors) standardized on a new talk (often called
  969.       ntalk) which knows about network byte order.  This talk works between
  970.       all machines that have it.  This version of talk uses port 518.
  971.  
  972.       There are now a few talk programs that speak both ntalk and one
  973.       version of otalk.  The most common of these is called "ytalk".
  974.  
  975. ------------------------------
  976.  
  977. End of unix/faq Digest part 2 of 7
  978. **********************************
  979.  
  980. -- 
  981. Ted Timar - tmatimar@isgtec.com
  982. ISG Technologies Inc., 6509 Airport Road, Mississauga, Ontario, Canada L4V 1S7
  983.  
  984.